package net.arvin.mobilebanksystem.fragment;

import net.arvin.dao.ResponseHandler;
import net.arvin.mobilebanksystem.R;
import net.arvin.mobilebanksystem.activity.UserInfoActivity;
import net.arvin.service.UserService;
import net.arvin.utils.NetConnectionUtil;
import net.arvin.utils.PreferenceUtils;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("ValidFragment")
public class ResetPSWFragment extends Fragment implements OnClickListener {
	private EditText password_et;
	private EditText repassword_et;
	private EditText answer_et;
	private TextView question_tv;
	private Button ensure;
	private String phonenumber;
	//默认为true
	private boolean isFromUserInfoActivity = true;;

	private ResponseHandler mResponseHandler;

	public ResetPSWFragment() {

	}

	public ResetPSWFragment(boolean isFromUserInfoActivity) {
		this.isFromUserInfoActivity = isFromUserInfoActivity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_resetpsw, null);
		init(view);
		return view;
	}

	private void init(View view) {
		initView(view);
		initListener();
	}

	private void initListener() {
		ensure.setOnClickListener(this);
	}

	private void initView(View view) {
		password_et = (EditText) view.findViewById(R.id.password_et);
		repassword_et = (EditText) view.findViewById(R.id.repassword_et);
		answer_et = (EditText) view.findViewById(R.id.answer_et);
		question_tv = (TextView) view.findViewById(R.id.question_tv);
		ensure = (Button) view.findViewById(R.id.ensure);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ensure:
			ensureValidate();
			break;
		}
	}

	public String getPhonenumber() {
		phonenumber = PreferenceUtils.getPreferPhonenumber(getActivity());
		return phonenumber;
	}

	public void setPhonenumber(String phonenumber) {
		this.phonenumber = phonenumber;
	}

	private void ensureValidate() {
		String answer = answer_et.getText().toString().trim();
		if (answer == null || answer.equals("")) {
			Toast.makeText(getActivity(), "密保答案不能为空~", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		String password = password_et.getText().toString().trim();
		if (password == null || password.trim().equals("")) {
			Toast.makeText(getActivity(), "密码不能为空~", Toast.LENGTH_SHORT).show();
			return;
		}
		if (!password.matches("^[a-zA-Z0-9]\\w{3,15}$")) {
			Toast.makeText(getActivity(), "密码只能是4-16为之间的数字、字母组成~",
					Toast.LENGTH_SHORT).show();
			return;
		}
		String repassword = repassword_et.getText().toString().trim();
		if (repassword == null || repassword.trim().equals("")) {
			Toast.makeText(getActivity(), "确认密码不能为空~", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (!repassword.equals(password)) {
			Toast.makeText(getActivity(), "确认密码和密码不一致", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (NetConnectionUtil.isConnectWithTip(getActivity(), "没有联网=_=!")) {
			UserService.tryResetPSW(getPhonenumber(), password, question_tv
					.getText().toString(), answer, getResponseHandler(),
					getActivity());
		}
	}

	private void back2Login() {
		if (isFromUserInfoActivity) {
			int backStackCount = getFragmentManager().getBackStackEntryCount();
			for (int i = 0; i < backStackCount; i++) {
				getFragmentManager().popBackStack();
			}
			UserInfoActivity.userinfoActivity.hidenTitleBar();
			UserInfoActivity.userinfoActivity
					.switchContent(new LoginFragment());
		} else {
			Intent tent = new Intent(getActivity(), UserInfoActivity.class);
			getActivity().startActivity(tent);
			getActivity().finish();
		}
	}

	private ResponseHandler getResponseHandler() {
		if (mResponseHandler == null) {
			mResponseHandler = new ResponseHandler() {
				@Override
				public void onRequsetSucceeded(final Object data) {
					Toast.makeText(getActivity(), "修改成功", Toast.LENGTH_SHORT)
							.show();
					back2Login();
				}

				@Override
				public void onRequestFailed(final Object data) {
					Toast.makeText(getActivity(), "服务器出错!", Toast.LENGTH_SHORT)
							.show();
				}
			};
		}
		return mResponseHandler;
	}
}
